#******************************************************************************
#******************************************************************************

# import libraries

import math

from src import pysoleng as pse

# test using "!python -m pytest -s --cov --cov-report term-missing"

#******************************************************************************
#******************************************************************************

class TestSolarDataFrame:
    
    def test_solar_df(self):
        
        filename = 'tests/TMY/tmy_54.027_9.803_2005_2020.csv'
    
        sdf = pse.sdf.SolarDataFrame(filename=filename)
        
        # 1) add a two-axis tracking surface
        
        surface_name = 'surf1'
        
        sdf.add_tracking_surface(ground_reflectance=0.6,
                                 surface_key=surface_name)
        
        column_name = sdf.column_name(sdf.KW_PRE_G_TOTAL, surface_name)
            
        assert math.isclose(
            sum(sdf[column_name])*3600*2.7777778E-07,
            1447.1750408805633,
            abs_tol=10)
        
        # 2) add a azimuth tracking surface
        
        surface_name = 'surf2'
        
        sdf.add_tracking_surface(slope=30*math.pi/180, 
                                 ground_reflectance=0.6,
                                 surface_key=surface_name)
        
        column_name = sdf.column_name(sdf.KW_PRE_G_TOTAL, surface_name)
        
        assert math.isclose(
            sum(sdf[column_name])*3600*2.7777778E-07,
            1303.393606732405,
            abs_tol=10)
        
        # 3) add a slope tracking surface
        
        surface_name = 'surf3'
        
        sdf.add_tracking_surface(azimuth=-15*math.pi/180, 
                                 ground_reflectance=0.6,
                                 surface_key=surface_name)
        
        column_name = sdf.column_name(sdf.KW_PRE_G_TOTAL, surface_name)
        
        assert math.isclose(
            sum(sdf[column_name])*3600*2.7777778E-07,
            1135.5893171143216,
            abs_tol=10)
        
        # 4) add another fixed surface
        
        surface_name = 'surf4'
        
        sdf.add_fixed_surface(azimuth=-15*math.pi/180, 
                              slope=30*math.pi/180,
                              surface_key=surface_name)
                              
        column_name = sdf.column_name(sdf.KW_PRE_G_TOTAL, surface_name)
        
        assert math.isclose(
            sum(sdf[column_name])*3600*2.7777778E-07,
            1082.6460954903605,
            abs_tol=10)
        
        # TODO: force an error by using the tracking method for a fixed surface
        
        # error_raised = False
        # try:
        #     sdf.add_fixed_surface(azimuth, slope)
        # except Exception:
        #     error_raised = True
        # assert error_raised
        
# *****************************************************************************
# *****************************************************************************